package com.wuchao.convert.service;


import cn.hutool.core.date.DateUtil;
import com.wuchao.convert.CommandManager;
import com.wuchao.convert.config.serviceConfig;
import com.wuchao.convert.data.CommandTasker;
import com.wuchao.convert.data.GetIpMain;
import com.wuchao.convert.entity.Device;
import com.wuchao.convert.mapper.DeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author WuChao
 * @version 1.0
 * @date 2020/4/3 11:06
 */
@Service
public class DeviceService {
    @Value("${convert.ip}")
    private String ip;
    @Value("${convert.port}")
    private String port;

    @Autowired
    private CommandManager commandManager;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private serviceConfig Config;


    public Device selectByPrimaryKey(String did) {
        return deviceMapper.selectByPrimaryKey(did);
    }


    public Map<String, String> startConvert(String did) throws SocketException, UnknownHostException {
        Device device = deviceMapper.selectByPrimaryKey(did);
        if (device != null) {
            //设备码流
            String deviceUri = device.getDeviceUri();
            Map<String, String> map = new HashMap<String, String>();
            map.put("appName", UUID.randomUUID().toString());
            map.put("rtsp_transport", Config.getRtsp_transport());
            map.put("input", deviceUri);
            map.put("output", Config.getOutput());
            map.put("codec",Config.getCodec());
            map.put("fmt", Config.getFmt());
            map.put("fps", Config.getFps());
            map.put("bv", Config.getBv());
            map.put("rs", Config.getRs());
            map.put("twoPart", "0");
            // 执行任务，id就是appName，如果执行失败返回为null
            String id = commandManager.start(map);
            System.out.println(id);
            // 通过id查询
            CommandTasker info = commandManager.query(id);
            System.out.println(info);
            // 查询全部
            Collection<CommandTasker> infoList = commandManager.queryAll();
            System.out.println(infoList);
            Map<String, String> resultMap = new HashMap<>();
            String ipAdd = GetIpMain.getIpAdd();
            if (ip==null) {
                ip = ipAdd;
            }
            resultMap.put("httpUrl", "http://" + ip +":"+ port + "/live?app=myapp&stream=" + id);
            resultMap.put("id", id);
            return resultMap;
        }else{
            throw new RuntimeException("无此设备开启失败");
        }


    }


    public Boolean stopConvert(String id) {

        boolean stop = commandManager.stop(id);
        return stop;

    }


    public String replayConvert(String did, String startTime, String endTime) {
        Device device = deviceMapper.selectByPrimaryKey(did);
        if (device == null) {
            throw new RuntimeException("无此设备开启失败");
        }
//        String replace = device.getDeviceUri().replace("realmonitor", "playback"); + "/cam/playback?channel=2&subtype=0";
        String replace = device.getDeviceUri();
       startTime = DateUtil.parse(startTime, "yyyy-MM-dd HH:mm:ss").toString("yyyy_MM_dd_HH_mm_ss");
       endTime=DateUtil.parse(endTime, "yyyy-MM-dd HH:mm:ss").toString("yyyy_MM_dd_HH_mm_ss");
        String rtsp = replace + "&starttime=" + startTime + "&endtime=" + endTime + "";
        //设备码流
        Map<String, String> map = new HashMap<String, String>();
        map.put("appName", UUID.randomUUID().toString());
        map.put("rtsp_transport", Config.getRtsp_transport());
        map.put("input", rtsp);
        map.put("output", Config.getOutput());
        map.put("codec",Config.getCodec());
        map.put("fmt", Config.getFmt());
        map.put("fps", Config.getFps());
        map.put("bv", Config.getBv());
        map.put("rs", Config.getRs());
        map.put("twoPart", "0");
        // 执行任务，id就是appName，如果执行失败返回为null
        String id = commandManager.start(map);
        System.out.println(id);
        // 通过id查询
        CommandTasker info = commandManager.query(id);
        System.out.println(info);
        // 查询全部
        Collection<CommandTasker> infoList = commandManager.queryAll();
        System.out.println(infoList);
        return id;
    }
}
